import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.sql.*;

public class Insert extends HttpServlet
{
        public void doPost(HttpServletRequest req,HttpServletResponse res)
        {
                Connection con = null;
                PreparedStatement ps = null;
                PreparedStatement ps1 = null;
                int rs = 0;
                try
                {
                        PrintWriter pw = res.getWriter();
                        res.setContentType("text/html");
                        
                        
                        String user_id = req.getParameter("User_Id");
                        String password = req.getParameter("Pass_word");
                        String fname = req.getParameter("FirstName");
                        String lname = req.getParameter("LastName");
                        String city = req.getParameter("City");
                        String country = req.getParameter("Country");
                        long pin = Long.parseLong(req.getParameter("ZIP"));
                        long contact = Long.parseLong(req.getParameter("PhoneNo"));
                        String gender = req.getParameter("sex");
                        System.out.println("Gender in servlet: "+gender);
                        System.out.println(gender);
                        Class.forName("oracle.jdbc.driver.OracleDriver");
                        con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","admin");
                        System.out.println("connection");
                        Statement st = con.createStatement();
                        ResultSet rs1=st.executeQuery("select user_id from pass_profile where user_id like '"+'%'+user_id+'%'+"'");
                        if(rs1.next())
                        {
                                 res.sendRedirect("RegError1.jsp");
                                 
                        }
                        else
                        {
                            ps = con.prepareStatement("insert into pass_profile values(?,?,?,?,?,?,?,?,?)");
                            ps.setString(1,user_id);
                            ps.setString(2,password);
                            ps.setString(3,fname);
                            ps.setString(4,lname);
                            ps.setString(5,city);
                            ps.setString(6,country);
                            ps.setLong(7,pin);
                            ps.setLong(8,contact);
                            ps.setString(9,gender);
                            rs = ps.executeUpdate();
                            if(rs!=1)
                            {
                                pw.println("<h2>Record Has Not Been Added Successfully</h2>");
                            }
                            else
                            {
                                 res.sendRedirect("Login2.jsp");
                            }
                        ps.close();
                        con.close();
                        }
                }
                catch(Exception e)
                {
                        e.printStackTrace();
                }
        }
        public void doGet(HttpServletRequest req,HttpServletResponse res)
        {
                try
                {
                        doPost(req,res);
                }
                catch(Exception e)
                {
                        e.printStackTrace();
                }
        }
}
